package di

import (
	"database/sql"
	"github.com/mix-go/xdi"
	"hy_808/logdot"
	"log"
	"os"
	"path/filepath"
)

func init() {
	obj := xdi.Object{
		Name: "sqlite",
		New: func() (interface{}, error) {
			var ProgramDir = func() string {
				path, _ := filepath.Abs(os.Args[0])
				return filepath.Dir(path) + logdot.PathSep
			}()
			db, err := sql.Open("sqlite", ProgramDir+"/runtime/data.db")
			if err != nil {
				log.Fatal("打开sql失败", err)
			}
			return db, nil
		},
	}
	if err := xdi.Provide(&obj); err != nil {
		panic(err)
	}
}

func Sqlite() (db *sql.DB) {
	if err := xdi.Populate("sqlite", &db); err != nil {
		panic(err)
	}
	return
}
